package com.xxx.video;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;

import com.xxx.video.commen.utils.ListUtils;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;


public class VideoApplication extends Application {
    private static Handler mainHandler;
    private static Context instance;
    public static List<Activity> activityList = new ArrayList<>();

    public static Activity getTopActivity() {
        if (ListUtils.isEmpty(activityList)) {
            return null;
        }
        return activityList.get(0);
    }

    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;
        mainHandler = new Handler();
        registerActivityLifecycleCallback();
    }

    /**
     * get single context
     *
     * @return context
     */
    public static Context getInstance() {
        return instance;
    }

    /**
     * get main handler
     */
    public static Handler getMainHandler() {
        return mainHandler;
    }

    private void registerActivityLifecycleCallback() {
        registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
            @Override
            public void onActivityCreated(@NotNull Activity activity, Bundle savedInstanceState) {
                activityList.add(0, activity);
            }

            @Override
            public void onActivityStarted(@NotNull Activity activity) {

            }

            @Override
            public void onActivityDestroyed(@NotNull Activity activity) {
                activityList.remove(activity);
            }

            @Override
            public void onActivityResumed(@NotNull Activity activity) {
            }

            @Override
            public void onActivitySaveInstanceState(@NotNull Activity activity, @NotNull Bundle outState) {

            }

            @Override
            public void onActivityStopped(@NotNull Activity activity) {

            }

            @Override
            public void onActivityPaused(@NotNull Activity activity) {

            }
        });

    }
}
